package com.zhb86.live.panel;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhb86.live.R;
import com.zhb86.live.adapter.GiftAdapter;
import com.zhb86.live.model.Gift;

import java.util.ArrayList;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by duanliuyi on 2018/5/11.
 */

public class GiftPanel extends LinearLayout {

    private RecyclerView recyclerView;
    private GiftAdapter adapter;
    private TextView tvGiftSend;
    private EditText editNum;

    private ArrayList<Gift> gifts;


    private int currentPosition = -1;


    public GiftPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }


    private void initView(final Context context) {
        final View layout = LayoutInflater.from(getContext()).inflate(R.layout.live_widget_gift_panel, this);

        recyclerView = (RecyclerView) findViewById(R.id.gv_gift);
        tvGiftSend = (TextView) findViewById(R.id.tv_gift_send);
        editNum = (EditText) findViewById(R.id.edit_gift_num);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        gifts = new ArrayList<>();
        gifts.add(new Gift());gifts.add(new Gift());
        adapter = new GiftAdapter(R.layout.live_item_gift,gifts);
        adapter.bindToRecyclerView(recyclerView);



        tvGiftSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(editNum.getText()) || Integer.parseInt(editNum.getText().toString()) == 0) {
                    Toast.makeText(context, "礼物数不能为0", Toast.LENGTH_SHORT).show();
                } else if (Integer.parseInt(editNum.getText().toString()) > 99) {
                    Toast.makeText(context, "礼物数不能超过99", Toast.LENGTH_SHORT).show();
                } else {
                    Gift gift = gifts.get(currentPosition);
                   /* ChatroomGift giftMessage = new ChatroomGift();
                    giftMessage.setId(gift.getGiftId());
                    giftMessage.setNumber(Integer.parseInt(editNum.getText().toString()));
                    ChatroomKit.sendMessage(giftMessage);*/
                    layout.setVisibility(GONE);
                }
            }
        });

    }
}
